﻿#include "ceshi01.h"
#include<qrect.h>
#include<qtimer.h>
#include<QKeyEvent>
#include<ctime>
#include<QPushButton>
#include<qpainter.h>
#ifdef _MSC_VER
#pragma execution_character_set("utf-8")
#endif //_MSC_VER

ceshi01::ceshi01(QWidget* parent)
    : QMainWindow(parent)
    , ui(new  Ui::ceshi01Class), blsRun(false), speed(100), fenshu1(1)
    , blsOver(false), qian(4), nDirection(4)

{
    ui->setupUi(this);
    this->setGeometry(QRect(300, 70, 1000, 700));//相对电脑左上角坐标，矩形长宽
}
ceshi01::~ceshi01()
{
    delete ui;
}
void ceshi01::paintEvent(QPaintEvent* event) {//update()或程序第一次启动时自动执行，用于画图
    QPainter painter(this);
    if (!blsRun) {//只blsRun为false时才初始化蛇
        Initceshi01();//初始化蛇，只是初始化了矩形，没有显示
        //btn->resize(100,50);
    }

    //画游戏背景
    //外墙
    painter.setPen(Qt::black);//画笔颜色
    painter.setBrush(Qt::gray);//填充颜色
    painter.drawRect(10, 10, 980, 680);//矩形，相对于起点坐标为（15，15）大小970*720
    //内墙
    painter.setPen(Qt::black);//画笔颜色
    painter.setBrush(Qt::black);//填充颜色
    painter.drawRect(20, 20, 960, 460);//矩形，相对于起点坐标为（15，15）大小960*660
    painter.drawPixmap(20, 20, 960, 460, QPixmap(":/images/new.png"));
    //画格子
    painter.setPen(Qt::cyan);//画笔颜色
    for (int i = 3; i <= 97; i++) {
        painter.drawLine(i * 10, 30, i * 10, 470);//竖线
    }
    for (int i = 3; i <= 47; i++) {
        painter.drawLine(30, i * 10, 970, i * 10);//横线坐标可计算
    }
    //显示游戏开始、结束
    QFont font1("Courier", 100);
    painter.setFont(font1);
    painter.setPen(Qt::red);
    painter.setBrush(Qt::red);
    painter.drawText(40, 150, sDisplay);
    //显示“得分”
    QFont font2("Courier", 100);
    painter.setFont(font2);
    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    painter.drawText(120, 630, xianshi);
    //显示得分
    QFont font3("Courier", 50);
    painter.setFont(font3);
    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    painter.drawText(480, 610, QString::number(fenshu1));
    /*painter.drawText(480, 570, QString::number(fenshu2));
    painter.drawText(480, 610, QString::number(fenshu3));
    painter.drawText(480, 650, QString::number(fenshu4));
    QFont font4("Courier", 35);
    painter.setFont(font4);
    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    painter.drawText(450, 575, chenghao);
    painter.drawText(450, 615, chenghao);
    painter.drawText(450, 655, chenghao);*/
    //画蛇
    painter.setPen(Qt::black);
    painter.setBrush(Qt::yellow);
    painter.drawRects(&vSnakeRect[0], vSnakeRect.size());//画n个小方块
    painter.setPen(Qt::black);
    painter.setBrush(QColor(255,200,0));
    painter.drawRect(shetou);
    //画食物
    painter.setPen(Qt::black);
    painter.setBrush(Qt::red);
    painter.drawRect(food);
    painter.drawRect(food2);
    if (blsOver)//游戏结束，停止计时
        timer->stop();

}
void ceshi01::Initceshi01() {
    //nDirection = 4;//默认向右运动
    blsRun = true;
    sDisplay = "开始游戏";
    //connect(btn,SIGNAL(&QPushButton->clicked),SLOT(isstart));
    food = CreateRect();//创建食物
    food2 = CreateRect2();//再创建一个食物
    xianshi = "得分";
    //chenghao = "";//乘号
    //QRect rect(100,100,10,10);//蛇的初始位置
    //vsnakeRect=rect;
    vSnakeRect.resize(5);//初始化长度为5
    //for循环初始化vector
    for (int i = 0; i < vSnakeRect.size(); i++) {
        QRect rect(100, 70 + 10 * i, 10, 10);//生成小方块
        vSnakeRect[vSnakeRect.size() - 1 - i] = rect;//小方块赋值到容器
    }
    shetou = vSnakeRect.first();//指定蛇头位置
    timer = new QTimer(this);//设定计时器
    timer->start(speed);//计时器时间是1s
    connect(timer, SIGNAL(timeout()), SLOT(Snake_update()));//信号连接槽
    //发出信号的是timer，信号是timeout，执行的槽是sheupdate()
}
void ceshi01::Snake_update() {
    sDisplay = "";
    shetou = vSnakeRect.first();//获取到蛇头
    iseat();
    ishit();
    for (int j = 0; j < vSnakeRect.size() - 1; j++) {
        vSnakeRect[vSnakeRect.size() - 1 - j] = vSnakeRect[vSnakeRect.size() - 2 - j];//让数组移动
    }
    if (qian == 5 - nDirection) {
        nDirection = qian;
    }
    qian = nDirection;
    switch (nDirection) {
    case 1:
        shetou.setTop(shetou.top() - 10);
        shetou.setBottom(shetou.bottom() - 10);
        break;
    case 4:
        shetou.setTop(shetou.top() + 10);
        shetou.setBottom(shetou.bottom() + 10);
        break;
    case 3:
        shetou.setLeft(shetou.left() - 10);
        shetou.setRight(shetou.right() - 10);
        break;
    case 2:
        shetou.setLeft(shetou.left() + 10);
        shetou.setRight(shetou.right() + 10);
        break;
    default:;
    }
    vSnakeRect[0] = shetou;
    if (shetou.left() < 30 || shetou.right() > 970 || shetou.top() < 30 || shetou.bottom() > 470) {
        sDisplay = "已经结束咧";
        blsOver = true;
    }
    update();//paintevent更新
}
void ceshi01::keyPressEvent(QKeyEvent* event) {
    QKeyEvent* key = (QKeyEvent*)event;
    switch (key->key()) {
    case Qt::Key_Up:nDirection = 1;   //1代表上
        break;
    case Qt::Key_Down:nDirection = 4;   //4代表下
        break;
    case Qt::Key_Left:nDirection = 3;   //3代表左
        break;
    case Qt::Key_Right:nDirection = 2;   //2代表右
        break;
    default:;
    }
}
QRect ceshi01::CreateRect() {
    int x, y;
    srand((unsigned int)time(NULL));
    x = rand() % 47 + 1;//生成小于95的整数//94
    y = rand() % 44 + 1;//44
    QRect rect(20 + x * 10, 20 + y * 10, 10, 10);//食物方块位置
    return rect;
}
QRect ceshi01::CreateRect2() {
    int h, i;
    srand((unsigned int)time(NULL));
    h = rand() % 47 + 48;//生成小于95的整数94
    i = rand() % 44 + 1;//44
    QRect rect(20 + h * 10, 20 + i * 10, 10, 10);//食物方块位置
    return rect;
}
void ceshi01::iseat() {
    if (shetou == food) {
        if (speed > 25) {
            speed -= 25;
            timer->stop();//停止当前计时器
            timer->start(speed);//并设置新的
        }
        shetou = food;
        vSnakeRect.push_back(vSnakeRect.last());//将最后一个节点再次加到容器里，变长
        int a = 3;
        if (food.left() > 350 && food.right() < 550 && food.top() > 170 && food.bottom() < 330)
        {
            a = 1;
        }
        else if (food.left() > 200 && food.right() < 800 && food.top() > 100 && food.bottom() < 400)
        {
            a = 2;
        }
        food = CreateRect();
        if (fenshu1 < 3000000000000000000) {
            fenshu1 *= a;//得分
        }
        /*else if (fenshu2 < 3000000000000000000) {
            fenshu2 *= a;
        }
        else if (fenshu3 < 3000000000000000000) {
            fenshu3 *= a;
        }
        else if (fenshu4 < 3000000000000000000) {
            fenshu4 *= a;
        }*/
    }
    if (shetou == food2) {
        if (speed > 25) {
            speed -= 25;
            timer->stop();//停止当前计时器
            timer->start(speed);//并设置新的
        }
        shetou = food2;
        vSnakeRect.push_back(vSnakeRect.last());//将最后一个节点再次加到容器里，变长
        int a = 3;
        if (food2.left() > 350 && food2.right() < 550 && food2.top() > 200 && food2.bottom() < 300)
        {
            a = 1;
        }
        else if (food2.left() > 200 && food2.right() < 800 && food2.top() > 100 && food2.bottom() < 400)
        {
            a = 2;
        }
        food2 = CreateRect2();
        if (fenshu1 < 3000000000000000000) {
            fenshu1 *= a;//得分
        }
        /*else if (fenshu2 < 3000000000000000000) {
            fenshu2 *= a;
        }
        else if (fenshu3 < 3000000000000000000) {
            fenshu3 *= a;
        }
        else if (fenshu4 < 3000000000000000000) {
            fenshu4 *= a;
        }*/
    }
}
void ceshi01::ishit() {
    for (int i = 1; i < vSnakeRect.size(); i++) {//遍历蛇身
        if (shetou == vSnakeRect[i]) {//判断重合
            sDisplay = "已经结束咧";
            blsOver = true;
            update();
        }
    }
}
